import java.awt.geom.Point2D;
import java.text.DecimalFormat;
import java.util.ArrayList;

import com.jhlabs.map.proj.MercatorProjection;

public class Station {
	private int child_id;
	private int parent_id;
	private String name;
	private String line;
	private String direction;
	private double lon;
	private double lat;
	private double x;
	private double y;
	private ArrayList<Integer> schedule;
	
	public Station(int child_id, int parent_id, String name, String direction, double lon, double lat, String line) {
		this.child_id = child_id;
		this.parent_id = parent_id;
		this.name = name;
		this.direction = direction;		    
		this.lon = lon;
		this.lat = lat;
		this.line = line;
		schedule = new ArrayList<Integer>();
	}
	
	public void setLon(double x) {
		lon = x;
	}
	
	public void setLat(double y) {
		 lat = y;
	}
	
	public void setX(double x) {
		this.x = x;
	}
	
	public void setY(double y) {
		 this.y = y;
	}
	
	public double getX() {
		return x;
	}
	
	public double getY() {
		 return y;
	}
	
	
	public int getChildid() {
		return child_id;
	}
	
	
	public int getParentid() {
		return parent_id;
	}
	
	public String getName() {
		return name;
	}
	
	
	public String getLine() {
		return line;
	}
	
	public double getLon() {
		return lon;
	}
	
	public double getLat() {
		return lat;
	}
	
	public String getDirection() {
		return direction;
	}
	
	/* Discards seconds from the arrival time to avoid overflow into minutes and hours caused by rounding up*/
	public void addTime(String time) {
		String[] tokens = time.trim().split(":");
		
		Integer hr = new Integer(tokens[0]);
		// Some arrival times formats use both 0 and 24 as 12AM
		if(hr >= 24) {
			hr -= 24;
		}
		Integer mins = new Integer(tokens[1]);
		
		schedule.add(new Integer(hr*60 + mins));
	}
	
	// TODO Handle overflow from 23 to 0
	public int getClosestTime(int mins) {
		int distance = Integer.MAX_VALUE;
		int result = 0;
		for(Integer time : schedule) {
			int difference = time - mins;
			if(difference < 0 ) { continue; }// can't go back in time
			if(difference < distance) {
				distance = difference;
				result = time;
			}
		}
		
		// If a time wasn't found in that day, start at midnight and look at the next day
		if(distance == Integer.MAX_VALUE) {
			mins = 0;
			for(Integer time : schedule) {
				int difference = time - mins;
				if(difference < 0 ) { continue; }// can't go back in time
				if(difference < distance) {
					distance = difference;
					result = time+(24*60-mins);

				}
			}
		}
		
		return result;
	}
	
	public void printSchedule() {
		// TODO
	}
	
	public boolean sibling(Station other) {
		if(this.parent_id == other.parent_id) {
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public int hashCode() {
		String id = "" + this.child_id + "" + line;
		/*final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());*/
		return id.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		Station other = (Station)obj;
		if(this.hashCode() == other.hashCode()) {
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public String toString() {
		return child_id + " " + parent_id + " " + name + " " + line  + " " + direction + " " + lon + " " + lat;	
	}
}
